from flask import Blueprint, jsonify, request
from ..models.ammunition_depot import AmmunitionDepot
from .. import db

bp = Blueprint('ammunition_depots', __name__, url_prefix='/api/ammunition-depots')

@bp.route('', methods=['GET'])
def get_ammunition_depots():
    depots = AmmunitionDepot.query.all()
    return jsonify([depot.to_dict() for depot in depots])

@bp.route('/<int:id>', methods=['GET'])
def get_ammunition_depot(id):
    depot = AmmunitionDepot.query.get_or_404(id)
    return jsonify(depot.to_dict())

@bp.route('', methods=['POST'])
def create_ammunition_depot():
    data = request.get_json()
    depot = AmmunitionDepot(
        airport_id=data['airport_id'],
        name=data['name'],
        type=data['type'],
        area=data['area']
    )
    db.session.add(depot)
    db.session.commit()
    return jsonify(depot.to_dict()), 201

@bp.route('/<int:id>', methods=['PUT'])
def update_ammunition_depot(id):
    depot = AmmunitionDepot.query.get_or_404(id)
    data = request.get_json()
    
    depot.airport_id = data.get('airport_id', depot.airport_id)
    depot.name = data.get('name', depot.name)
    depot.type = data.get('type', depot.type)
    depot.area = data.get('area', depot.area)
    
    db.session.commit()
    return jsonify(depot.to_dict())

@bp.route('/<int:id>', methods=['DELETE'])
def delete_ammunition_depot(id):
    depot = AmmunitionDepot.query.get_or_404(id)
    db.session.delete(depot)
    db.session.commit()
    return '', 204 